package org.brisling.docManage.service;

import java.util.List;

import org.brisling.authorize.shiro.ShiroSecurityService;
import org.brisling.base.domain.ListDomain;
import org.brisling.base.domain.RetDomain;
import org.brisling.common.ConstSysParam;
import org.brisling.common.SysConfig;
import org.brisling.docManage.domain.DocRead;
import org.brisling.docManage.repository.DocreadRepo;
import org.brisling.sysman.domain.Users;
import org.brisling.sysman.service.base.BaseSysmanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;


@RestController
@RequestMapping("/docread")
/**
 * 文档阅读回执服務service
 * @Author : jackson wang
 * @Date   : 2016年8月20日 下午4:18:01
 * @version: 1.0
 */
public class DocreadService extends BaseSysmanService<DocRead>{
	
	
	@Autowired
	private	ShiroSecurityService shiroSecurityService;
	
//	@Autowired
//	private	QueryDocService qryDocService;
	@Autowired
	private DocdetailService docdetailService;
	
	@Autowired
	private DocreadRepo baseRepo;
	
	public DocreadService(){
		this.moduleName = ConstSysParam.MODULE_DOCREAD;
		this.subSystem = ConstSysParam.SUBSYSTEM_DOCUMENT;
		this.clazz = DocRead.class;
	}
	
	/**
	 * 获取业务主页面 
	 * @return
	 */
	@RequestMapping("/mainPage.action")
	public ModelAndView getMainPage(){
		return getBizMainPage();
	}
	
	/**
	 * 获取业务编辑页面
	 * @return
	 */
	@RequestMapping("/editPage.action")
	public ModelAndView getEditPage(){
		return getBizEditPage();
	}
	
	
	/**
	 * 分页查询用户列表页面
	 * @param deptid
	 * @param page
	 * @param rows
	 * @param sort
	 * @return
	 */
	@RequestMapping("/getListDomain.action")
	public ListDomain<DocRead> getRoleListDomain(
			@RequestParam(value="page",required=false) String page,
			@RequestParam(value="rows",required=false) String rows,
			@RequestParam(value="sort",required=false) String sort){
		
				
		return super.getPagedList(page, rows);
			
	}
	
	/**
	 * 分页查询用户列表页面
	 * @param deptid
	 * @param page
	 * @param rows
	 * @param sort
	 * @return
	 */
	@RequestMapping("/getList.action")
	public List<DocRead> getRoleList(
			@RequestParam(value="page",required=false) String page,
			@RequestParam(value="rows",required=false) String rows,
			@RequestParam(value="sort",required=false) String sort){
		
		
			return super.getAll(page,rows,getSort(sort));
			
	}
	
	
	/**
	 * 删除记录,单条或批量删除记录,id不为空，执行单条删除，id为空且ids不为空，执行批量删除
	 * @param id	待删除的单条记录id
	 * @param ids	待删除的多条记录id，以','分隔
	 * @return
	 */
	@RequestMapping("/del.action")
	public RetDomain delRow(@RequestParam(value="id",required=false) Long id,@RequestParam(value="ids",required=false) String ids){
				
		return super.delRows(id, ids);		 
	}
	
	/**
	 * 删除记录,单条或批量删除记录,id不为空，执行单条删除，id为空且ids不为空，执行批量删除
	 * @param id	待删除的单条记录id
	 * @param ids	待删除的多条记录id，以','分隔
	 * @return
	 */
	@RequestMapping("/flowDel.action")
	public RetDomain delFlowRow(@RequestParam(value="id",required=false) Long id,@RequestParam(value="ids",required=false) String ids){
		
			return delRow(id,ids);
		
	}
	
	
	/**
	 * 保存用户阅读回执
	 * @param id
	 */
	@RequestMapping("/save.action")
	public DocRead save(@RequestParam(value="id",required=false) Long docid){
		
		DocRead domain = null;
		if(docid!=null){
//			Docdetail doc = qryDocService.getOne(docid);
		
		
			//查找当前用户对特定id文档的阅读回执是否存在
			
			Long userid = shiroSecurityService.getCurrentUserid();
			domain = baseRepo.findByDocidAndUserid(docid, userid);
			if(domain==null || domain.getId()==null){
				domain = new DocRead();
				domain.setDocid(docid);
				
				Users usr = SysConfig.getUsr(shiroSecurityService.getCurrentUserIdentify());
				domain.setUserid(userid);
				domain.setUser_name(usr.getName());
				domain.setUser_deptid(usr.getDeptid());
				domain.setUser_deptname(usr.getDeptName());				
			}			
			domain = super.save(domain);
			List<DocRead> lists = baseRepo.findByDocid(docid);
			
			Integer cnt = null;
			if(lists!=null && lists.size()>0){
				cnt = lists.size();
			}
			docdetailService.updateCnt(docid,cnt);
		}		
		return domain;
	}	
}
